package com.csmz.mybookreader.View;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class NavigationView extends LinearLayout {
    private static final String TAG = "NavigationView";
    private static final int RIGHT = 100;
    private static final int MIN_VELOCITY = 300;
    private static float density;
    private ViewDragHelper mDragHelper;
    private View mContent;
    private View mMenu;

    public NavigationView(Context context) {
        this(context, null);
    }

    public NavigationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        mDragHelper = ViewDragHelper.create(this, new CustomCallBack());
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);//EDGE_LEFT
        density  = getResources().getDisplayMetrics().density;
    }

    private class CustomCallBack extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {

            return child == mMenu;
        }

        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
            mDragHelper.captureChildView(mMenu,pointerId);
        }


        //这个地方实际上left就代表 你将要移动到的位置的坐标。返回值就是最终确定的移动的位置。
        // 我们要让view滑动的范围在我们的layout之内
        //实际上就是判断如果这个坐标在layout之内 那我们就返回这个坐标值。
        //如果这个坐标在layout的边界处 那我们就只能返回边界的坐标给他。不能让他超出这个范围
        //除此之外就是如果你的layout设置了padding的话，也可以让子view的活动范围在padding之内的.
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            //取得左边界的坐标
            final int leftBound = getPaddingLeft();
            //取得右边界的坐标x
            final int rightBound = getWidth() - child.getWidth() - leftBound;
            //这个地方的含义就是 如果left的值 在leftbound和rightBound之间 那么就返回left
            //如果left的值 比 leftbound还要小 那么就说明 超过了左边界 那我们只能返回给他左边界的值
            //如果left的值 比rightbound还要大 那么就说明 超过了右边界，那我们只能返回给他右边界的值
            return Math.min(Math.max(left, leftBound), rightBound);

        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            invalidate();
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            if (xvel > MIN_VELOCITY || releasedChild.getLeft()  >-releasedChild.getWidth() * 0.5) {
                mDragHelper.settleCapturedViewAt(0, releasedChild.getTop());
            }else {
                mDragHelper.settleCapturedViewAt(-releasedChild.getWidth(), releasedChild.getTop());
            }
            invalidate();
            /*//mAutoBackView手指释放时可以自动回去
            if (releasedChild == mMenu) {
                mDragHelper.settleCapturedViewAt(200, 200);
                invalidate();
            }*/
        }
    }

    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)){
            invalidate();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int count = getChildCount();
        if(count >= 2){
            //简单写了  直接写死
            mMenu = getChildAt(1);
            mContent = getChildAt(0);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //如果menu的宽度是match_parent或者超过限制 那么就需要重新设置
        int width = (int) (density * RIGHT);
        if (mMenu.getMeasuredWidth() + width > getWidth()){
            int menuWidthSpec = MeasureSpec.makeMeasureSpec(getWidth() -width,MeasureSpec.EXACTLY);
            mMenu.measure(menuWidthSpec,heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (mMenu != null){
            mMenu.layout(-mMenu.getMeasuredWidth(),top,0,mMenu.getMeasuredHeight());
        }
        if (mContent != null){
            mContent.layout(0,0,mContent.getMeasuredWidth(),mContent.getMeasuredHeight());
        }

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean event = mDragHelper.shouldInterceptTouchEvent(ev);
        return event;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e(TAG,"onTouchEvent" + event.toString());
        mDragHelper.processTouchEvent(event);
        return true;
    }

}
